package com.xoa.service.seal;

import com.xoa.dao.seal.SealJingeKeyMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.seal.SealJingeKey;
import com.xoa.model.seal.SealJingeKeyExample;
import com.xoa.model.users.Users;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by 张雨 on 2018/1/12.
 * 金格电子印章密钥盘管理
 */
@Service
public class SealJingeKeyService {

    @Autowired
    private SealJingeKeyMapper sealJingeKeyMapper;

    @Autowired
    private UsersMapper usersMapper;

    //新增授权管理
    public ToJson<SealJingeKey> addSealJingeKeyList(HttpServletRequest request, SealJingeKey sealJingeKey){
        ToJson<SealJingeKey> toJson = new ToJson<>();
        Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
        Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
        sealJingeKey.setCreateUser(user.getUserId());
        sealJingeKey.setCreateTime(new Date());

        try {
            int result = sealJingeKeyMapper.insertSelective(sealJingeKey);
            if(result > 0) {
                toJson.setFlag(0);
                toJson.setMsg("删除成功!");
            } else {
                toJson.setFlag(1);
                toJson.setMsg("删除失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toJson;
    }

    //授权管理列表展示
    public ToJson<SealJingeKey> getSealJingeKeyList(Integer sealId){
        ToJson<SealJingeKey> toJson = new ToJson<>();

        SealJingeKeyExample example = new SealJingeKeyExample();
        example.or().andSealIdEqualTo(sealId);
        List<SealJingeKey> sealJingeKeyList = sealJingeKeyMapper.selectByExample(example);

        for(SealJingeKey sealJingeKey: sealJingeKeyList) {
            String userNames = "";
            String[] userIds = sealJingeKey.getUserIdStr().split(",");
            for(String userId: userIds) {
                String userName = usersMapper.getUsernameById(userId);
                if(userName != null) {
                    userNames += "," + userName;
                }
            }
            userNames = userNames.replaceFirst(",","");
            sealJingeKey.setUserNames(userNames);
        }
        if(sealJingeKeyList != null) {
            toJson.setFlag(0);
            toJson.setMsg("查询成功!");
            toJson.setObj(sealJingeKeyList);
        } else {
            toJson.setFlag(1);
            toJson.setMsg("查询失败!");
        }
        return toJson;
    }

    //查询授权
    public ToJson<SealJingeKey> getSealJingeKey(Integer keyId){
        ToJson<SealJingeKey> toJson = new ToJson<>();
        SealJingeKey sealJingeKey = sealJingeKeyMapper.getSealJinge(keyId);
        String userNames = "";
        String[] userIds = sealJingeKey.getUserIdStr().split(",");
        for(String userId: userIds) {
            String userName = usersMapper.getUsernameById(userId);
            if(userName != null) {
                userNames += "," + userName;
            }
        }
        userNames = userNames.replaceFirst(",","");
        sealJingeKey.setUserNames(userNames);
        if(sealJingeKey != null) {
            toJson.setFlag(0);
            toJson.setMsg("查询成功!");
            toJson.setObject(sealJingeKey);
        } else {
            toJson.setFlag(1);
            toJson.setMsg("查询失败!");
        }
        return toJson;
    }

    //修改授权
    @Transactional(rollbackFor = RuntimeException.class)
    public ToJson<SealJingeKey> updateSealJingeKey(SealJingeKey sealJingeKey){
        ToJson<SealJingeKey> toJson = new ToJson<>();

        try {
            int result = sealJingeKeyMapper.updateByPrimaryKeySelective(sealJingeKey);
            if(result > 0) {
                toJson.setFlag(0);
                toJson.setMsg("更新成功!");
            } else {
                toJson.setFlag(1);
                toJson.setMsg("更新失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("更新失败！");
        }
        return toJson;
    }

    //删除授权
    public ToJson<SealJingeKey> deleteSealJingeKey(Integer keyId){
        ToJson<SealJingeKey> toJson = new ToJson<>();

        try {
            int result = sealJingeKeyMapper.deleteByPrimaryKey(keyId);
            if(result > 0) {
                toJson.setFlag(0);
                toJson.setMsg("删除成功!");
            } else {
                toJson.setFlag(1);
                toJson.setMsg("删除失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toJson;
    }
}
